import Vue from "vue";

/**
 * 匹配电话号码
 * 15012341234 => 150****1234
 */
Vue.filter("phone", function (value) {
  if (!value) return "";
  let str = value;
  str = str.toString().replace(/^(\d{3})(\d{4})(\d{4})/g, "$1****$3");
  return str;
});

/**
 * 格式化电话号码
 * 15012341234 => 150 1234 1234
 */
Vue.filter("formatPhone", function (value) {
  if (!value) return "";
  let str = value;
  str = str.replace(/\s/g, "").replace(/(\d{3})(\d{0,4})(\d{0,4})/, "$1 $2 $3");
  return str;
});

/**
 * 匹配名字
 * 王仙女 => 王*女
 */
Vue.filter("name", function (value) {
  if (!value) return "";
  let str = value;
  str = str
    .toString()
    .replace(/^([^\x00-\xff])([^\x00-\xff]{0,})([^\x00-\xff])/g, "$1*$3");
  return str;
});

export default {
  init() {
    console.log("*** filter installed ***");
  },
};
